/****************************************************
	文件：CameraRenderer.Editor.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/9/9 16:13:21
	功能：处理单个摄像机的渲染（只作用于编辑器模式，实机模式无效）
*****************************************************/

using UnityEditor;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Rendering;

namespace LearningSRP
{
    public partial class CameraRenderer
    {
#if UNITY_EDITOR
        /// <summary>
        /// 遗留的 Shader 的 Tag Id
        /// </summary>
        private static ShaderTagId[] mLegacyShaderTagIds =
        {
            new ShaderTagId("Always"),
            new ShaderTagId("ForwardBase"),
            new ShaderTagId("PrepassBase"),
            new ShaderTagId("Vertex"),
            new ShaderTagId("VertexLMRGBM"),
            new ShaderTagId("VertexLM"),
        };
        /// <summary>
        /// 错误的材质
        /// </summary>
        private static Material mErrorMat;

        /// <summary>
        /// 绘制不支持的 Shader
        /// </summary>
        partial void DrawUnsupportedShaders()
        {
            if (mErrorMat == null)
            {
                mErrorMat = new Material(Shader.Find("Hidden/InternalErrorShader"));
            }
            DrawingSettings drawingSettings = new DrawingSettings(mLegacyShaderTagIds[0], new SortingSettings(mCamera))
            {
                overrideMaterial = mErrorMat, //不支持的 Shader 显示错误材质
            };
            for (int i = 1; i < mLegacyShaderTagIds.Length; i++)
            {
                drawingSettings.SetShaderPassName(i, mLegacyShaderTagIds[i]);
            }
            FilteringSettings filteringSettings = FilteringSettings.defaultValue;
            mContext.DrawRenderers(mCullingResults, ref drawingSettings, ref filteringSettings);
        }

        /// <summary>
        /// 渲染 Gizmos
        /// </summary>
        partial void DrawGizmos()
        {
            if (Handles.ShouldRenderGizmos())
            {
                mContext.DrawGizmos(mCamera, GizmoSubset.PreImageEffects);
                mContext.DrawGizmos(mCamera, GizmoSubset.PostImageEffects);
            }
        }

        /// <summary>
        /// 在 Scene 场景中渲染 UI
        /// </summary>
        partial void PrepareForSceneWindow()
        {
            if (mCamera.cameraType == CameraType.SceneView)
            {
                ScriptableRenderContext.EmitWorldGeometryForSceneView(mCamera);
            }
        }

        /// <summary>
        /// 准备 Command 缓冲
        /// </summary>
        partial void PrepareBuffer()
        {
            Profiler.BeginSample("Editor Only"); //编辑器模式下调试时显示
            mSampleName = mBuffer.name = mCamera.name;
            Profiler.EndSample();
        }
#endif

#if UNITY_EDITOR
        private string mSampleName { get; set; }
#else
        const string mSampleName = mBufferName;
#endif

        partial void DrawUnsupportedShaders();
        partial void DrawGizmos();
        partial void PrepareForSceneWindow();
        partial void PrepareBuffer();
    }
}
